﻿CREATE PROCEDURE [dbo].[usp_select_CanResubmitMessage]
(
       @MessageId    uniqueidentifier
      ,@CanResubmit  bit               OUTPUT
      ,@Debug        bit = 0
)
AS

/****************************************************************************
Name: usp_select_CanResubmitMessage.sql
Description:  This stored procedure will check if a faulted message can be 
resubmitted.
*****************************************************************************/
SET NOCOUNT ON;

-- Bootstrap
DECLARE @DebugPrefix       varchar(10);
DECLARE @ErrorCode         int;

SET @ErrorCode          = 0;
SET @DebugPrefix        = '>>> DEBUG: ';


-- Test harness
/*
DECLARE @Debug bit;
DECLARE @MessageId uniqueidentifier;
DECLARE @CanResubmit bit;

-- now set them
SELECT
	 @MessageId  = NEWID()
   ,@CanResubmit = 1
   ,@Debug                 = 1;

*/

-- Print parameters
IF (@Debug > 0)
BEGIN
    PRINT @DebugPrefix + '@MessageId = ' + CAST(@MessageID AS varchar(50));
END;


SELECT @CanResubmit = ~[ResubmitSuccessful]
FROM Message
WHERE [MessageID] = @MessageId;


IF (@Debug > 0)
BEGIN
    PRINT @DebugPrefix + '@CanResubmit = ' + CAST(@CanResubmit AS varchar);
END;

-- CHECK FOR ERROR
SET @ErrorCode = @@Error;
IF (@ErrorCode <> 0) GOTO ERROR_HANDLER;
			
RETURN 0;

-- Handles errors.
ERROR_HANDLER:
IF (@ErrorCode <> 0)
BEGIN
    IF (@Debug > 0) 
		BEGIN
			PRINT @DebugPrefix + 'Error Code = ' + CAST(@ErrorCode AS varchar(10)) + ' occurred.';
			RETURN @ErrorCode
		END

    -- Don't return any evidence of what specific error occurred, i.e. 
    -- we want to ward off potential hackers.
    RETURN 1;
END;

SET NOCOUNT OFF;
